package de.softcon.sc4retail.ui;

import de.softcon.sc4retail.ImageAdapter;
import de.softcon.sc4retail.R;
import de.softcon.sc4retail.R.id;
import de.softcon.sc4retail.R.layout;
import de.softcon.sc4retail.domainmodel.CartItem;
import de.softcon.sc4retail.domainmodel.ShoppingCart;
import de.softcon.sc4retail.domainmodel.Snowboard;
import de.softcon.sc4retail.domainmodel.Stock;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;

public class ShopItemDialog extends OptionMenuPlugin {
	//Caches the currently active cart item
	private CartItem currentItem = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);	   

	    Gallery gallery = (Gallery) findViewById(R.id.gallery);
	    gallery.setAdapter(new ImageAdapter(this));

	    gallery.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView parent, View v, int position, long id) {	
				Snowboard board = Stock.getInstance().getSnowboards().get(position);
				currentItem = board;
				
				((TextView)findViewById(R.id.boardName)).setText(board.getName());
				((TextView)findViewById(R.id.boardType)).setText(board.getType());
				((TextView)findViewById(R.id.boardSize)).setText(board.getSize());
				((TextView)findViewById(R.id.boardManufacturer)).setText(board.getManufacturer());
				((TextView)findViewById(R.id.boardPrice)).setText(String.valueOf(board.getPrice()));						
			}	    	
	    });
	    
	    //Add a listener for the Add to cart button
	    Button addToCartButton = (Button)findViewById(R.id.addToCartButton);
	    addToCartButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Add the currently active item to the shopping cart
				if(currentItem != null)
				{
					ShoppingCart.getInstance().addItemToCart(currentItem);
					Toast.makeText(ShopItemDialog.this, currentItem.getName() + " has been added to cart", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}


